package gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class SearchPanel extends JPanel implements ActionListener
{
	static final long serialVersionUID = 1L;
	JLabel searchLabel;
	JButton searchButton, buyButton;
	JTextField searchTextField;
	JTable table;
	JScrollPane scrollPane;
	Vector<Vector<Object>> data;
	
	GUI guiObject;
	JPanel callingPanel;
	
	private boolean robbieRotten;
	
	public void clearFields()
	{
		searchTextField.setText("");
	}

	public SearchPanel(GUI guiObject, JPanel callingPanel)
	{
		robbieRotten = callingPanel instanceof FrontPage;
		this.guiObject = guiObject;
		this.callingPanel = callingPanel;
		setLayout(new GridBagLayout());
		createSearchPanel();
		createTable();
		setMinimumSize(new Dimension(700, 500));
	}
	
	private void createSearchPanel()
	{
		searchLabel = new JLabel("Books On Sale: Search By ISBN, Author, Title");
		searchTextField = new JTextField();
		searchButton = new JButton("Search");
		
		searchButton.setActionCommand("search");
		searchButton.addActionListener(guiObject);
		searchButton.addActionListener(this);
		
		searchTextField.setPreferredSize(new Dimension(500, 20));
		
		Constraints.setConstraints(0,0,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);

		add(new JSeparator(SwingConstants.HORIZONTAL), Constraints.gbc);
		
		Constraints.setConstraints(0,1,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		
		add(searchLabel, Constraints.gbc);
		
		Constraints.setConstraints(0,2,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		
		add(searchTextField, Constraints.gbc);
		
		Constraints.setConstraints(1,2,1,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
		
		add(searchButton, Constraints.gbc);
	}
	
	private void createTable()
	{
        Vector<String> columnNames = new Vector<String>();
        columnNames.add("Book Name");
        columnNames.add("Author");
        columnNames.add("Edition");
        columnNames.add("Quality");
        columnNames.add("ISBN");
        columnNames.add("Price");

        data = new Vector<Vector<Object>>();
                
        data.addAll(guiObject.search(""));

        table = new JTable(data, columnNames) 
        {
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;   //Disallow the editing of any cell
              }
        };
        table.setPreferredScrollableViewportSize(new Dimension(600, 475));
        
        table.setFillsViewportHeight(true);
        //table.setGridColor(new Color(255,255,255));
        table.setShowGrid(false);
        
        if(robbieRotten)
        {
        	table.setColumnSelectionAllowed(false);
        	table.setRowSelectionAllowed(false);
        	//searchTextField.setText("Instance");
        }
        else
        {
    		buyButton = new JButton("Buy Book");
    		buyButton.setActionCommand("buybook");
    		buyButton.addActionListener(guiObject);
            
    		Constraints.setConstraints(1,1,1,1,1,1,GridBagConstraints.NORTH,
    				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
    				
    		add(buyButton, Constraints.gbc);
        }

        //Create the scroll pane and add the table to it.
        scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        
        Constraints.setConstraints(0,3,2,1,1,1,GridBagConstraints.NORTH,
				GridBagConstraints.BOTH, new Insets(5,0,5,0),0,0);
        
        add(scrollPane, Constraints.gbc);
    }

	public void actionPerformed(ActionEvent e)
	{
		if("search".equalsIgnoreCase(e.getActionCommand()))
		{
			//data.add(guiObject.search(searchTextField.getText()));
	        
			Vector<Vector<Object>> holder = guiObject.search(searchTextField.getText());
			
			data.clear();
			data.addAll(holder);
			
	        table.updateUI();
		}
	}
}
